import ViewBase from "./ViewBase";
import Common from "../common/Common";
import UIManager from "../manager/UIManager";
import Define from "../common/Define";
import WXHelper from "../common/WXHelper";
import PlayerData from "../PlayerData";
import ScollHelper from "../common/ScollHelper";
import RankingItem from "../RankingItem";
import HttpManager from "../manager/HttpManager";
import UserInfo from "../UserInfo";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class RankingViewCtr extends ViewBase {

    @property(cc.Node)
    btn_back: cc.Node = null;

    @property(cc.Node)
    btn_friend: cc.Node = null;

    @property(cc.Node)
    btn_world: cc.Node = null;

    @property(cc.Label)
    textVersion: cc.Label = null;
    

    scollHelper: ScollHelper = null;

    dataArr:PlayerData[] = [];

    isShowFriendTag:boolean = true;
    curTag:string = "";



    //点击点击事件
    addEvent(){
        console.log("监听了按键  MainView");
        Common.addClickEvent(this.btn_back,this.onClick.bind(this));     
        Common.addClickEvent(this.btn_world,this.onClick.bind(this),false);    
        Common.addClickEvent(this.btn_friend,this.onClick.bind(this),false);  
        this.textVersion.string = Define.strVersion;  
    }
    //显示排行榜
    refreshView(isFristRefresh:boolean = false){
        
        console.log("显示排行榜界面")
        if(isFristRefresh){
            this.onClick("btn_friend");
        }else{
            if(this.isShow){
                if(this.curTag == "btn_world"){
                    this.onClick("btn_world");
                }
            }
        }
    }

    onClick(tag:string){
        
        this.btn_world.children[0].color = cc.color(0,0,0);
        this.btn_friend.children[0].color = cc.color(0,0,0);

        if(this.scollHelper == null){
            this.scollHelper = this.node.getComponent<ScollHelper>(ScollHelper);
        }
        this.scollHelper.scollView.node.active = false;
        this.curTag = tag
        if(tag == "btn_back"){
            if(this.isShowFriendTag){
                WXHelper.instance.hideView("Ranking");
            }
            this.dataArr = []
            UIManager.getInstance().hideView(Define.viewRanking);
            UIManager.getInstance().showView(Define.viewMain)
            UIManager.getInstance().showView(Define.viewUpgrade)
        }else if(tag == "btn_world"){
            //说明没有登录
/*
            for (let index = 0; index < 8; index++) {
                let tempdata = new PlayerData();
                tempdata.weekDate = "1" ;
                tempdata.isSelf = false;
                tempdata.avatarUrl = ""
                tempdata.nickName = "sss"
                this.dataArr.push(tempdata);
            }
            this.showWolrdRanking(this.dataArr)*/
            
            if(UserInfo.openid == ""){
                WXHelper.wxCheckLogin(false);
                this.btn_friend.children[0].color = cc.color(255,255,255);
                return;
            }

            this.isShowFriendTag = false
            console.log("ssssssssssssssssssssssss     222")
            WXHelper.instance.hideView("Ranking");
            console.log("ssssss 88888888888888888")
            if(this.dataArr.length == 0){
                HttpManager.getInstance().ranking(function(dataArr:PlayerData[]){
                    this.dataArr = dataArr;
                    if(this.dataArr.length > 0){
                        this.showWolrdRanking(this.dataArr)
                    }
                }.bind(this));
            }else{
                this.showWolrdRanking(this.dataArr)
            }
            this.btn_world.children[0].color = cc.color(255,255,255);
        }else if(tag == "btn_friend"){
            this.isShowFriendTag = true
            WXHelper.instance.showView("Ranking","friend");
            this.btn_friend.children[0].color = cc.color(255,255,255);
        }
    }

    showWolrdRanking(dataList:PlayerData[]){

        this.scollHelper.scollView.node.active = true;
        this.scollHelper.initItems(dataList.length, this.refreshItem.bind(this));
        this.node.active = true
    }

    refreshItem(idx:number, objIdx:number, obj:cc.Node){
        if(idx >= this.dataArr.length){
            return
        }
        console.log("idx ============= " +idx);
        console.log("this.dataList = " +this.dataArr.length);
        let item:RankingItem = obj.getComponent<RankingItem>(RankingItem);
        item.setRankingInfo(this.dataArr[idx])
    }
}
